# coding=utf8
from app.extensions import db
from app.modules.common.utils import ComplexEncoder
from sqlalchemy.sql import func
from app.modules.common.dbUtil import createId

"""
申请记录
"""


class ApplyRecord(db.Model, ComplexEncoder):
    __tablename__ = "apply_record"
    record_id = db.Column(db.String, primary_key = True)
    create_date = db.Column(db.DateTime, default = func.now())
    plan_id = db.Column(db.String)
    plan_name = db.Column(db.String)
    applicant_id = db.Column(db.String)
    applicant_name = db.Column(db.String)
    start_date = db.Column(db.Date)
    expect_recover_date = db.Column(db.Date)
    desc = db.Column(db.String)
    status = db.Column(db.String)
    agent_ip = db.Column(db.String)
    agent_ip_bak = db.Column(db.String)
    agent_status = db.Column(db.Integer)
    apply_load_level_name = db.Column(db.String)
    added_desc = db.Column(db.String)
    audit_id = db.Column(db.String)
    audit_name = db.Column(db.String)
    type = db.Column(db.String)
    isUseWeb = db.Column(db.String)
    useWebDesc = db.Column(db.String)
    useOldEnv = db.Column(db.String)
    isIntegration = db.Column(db.String)
    isKeep = db.Column(db.String)
    agent_status_desc = None
    hand_time = db.Column(db.DateTime)
    recover_time = db.Column(db.DateTime)
    audit_time = db.Column(db.DateTime)

    def __init__(self, plan_id = None, plan_name = None, applicant_id = None, applicant_name = None,
                 start_date = None, expect_recover_date = None, desc = None, status = None, agent_ip = None,
                 agent_status = None, apply_load_level_name = None, added_desc = None, audit_id = None,
                 audit_name = None, type = None, agent_status_desc = None, isUseWeb = '1', useWebDesc = None,
                 isIntegration = '1', useOldEnv = '1', isKeep = '1', hand_time = None, recover_time = None,
                 agent_ip_bak = None, audit_time = None):
        self.record_id = createId("ap")
        self.plan_id = plan_id
        self.plan_name = plan_name
        self.applicant_id = applicant_id
        self.applicant_name = applicant_name
        self.start_date = start_date
        self.expect_recover_date = expect_recover_date
        self.desc = desc
        self.status = status
        self.agent_ip = agent_ip
        self.agent_ip_bak = agent_ip_bak
        self.agent_status = agent_status
        self.apply_load_level_name = apply_load_level_name
        self.added_desc = added_desc
        self.audit_id = audit_id
        self.audit_name = audit_name
        self.type = type
        self.agent_status_desc = agent_status_desc
        self.isUseWeb = isUseWeb
        self.useWebDesc = useWebDesc
        self.isIntegration = isIntegration
        self.isKeep = isKeep
        self.useOldEnv = useOldEnv
        self.hand_time = hand_time
        self.recover_time = recover_time
        self.audit_time = audit_time

    def to_dict(self):
        return {
            'record_id': self.record_id,
            'create_date': self.create_date.strftime('%Y-%m-%d %H:%M:%S') if self.create_date else None,
            'hand_time': self.hand_time.strftime('%Y-%m-%d %H:%M:%S') if self.hand_time else None,
            'recover_time': self.recover_time.strftime('%Y-%m-%d %H:%M:%S') if self.recover_time else None,
            'audit_time': self.audit_time.strftime('%Y-%m-%d %H:%M:%S') if self.audit_time else None,
            'plan_id': self.plan_id,
            'plan_name': self.plan_name,
            'applicant_id': self.applicant_id,
            'applicant_name': self.applicant_name,
            'start_date': self.start_date.strftime('%Y-%m-%d') if self.start_date else None,
            'expect_recover_date': self.expect_recover_date.strftime(
                '%Y-%m-%d') if self.expect_recover_date else None,
            'desc': self.desc,
            'status': self.status,
            'agent_ip': self.agent_ip,
            'agent_ip_bak': self.agent_ip_bak,
            'agent_status': self.agent_status,
            'added_desc': self.added_desc,
            'audit_id': self.audit_id,
            'audit_name': self.audit_name,
            'agent_status_desc': self.agent_status_desc,
            'apply_load_level_name': self.apply_load_level_name,
            'type': self.type,
            'isUseWeb': self.isUseWeb,
            'useWebDesc': self.useWebDesc,
            'useOldEnv': self.useOldEnv,
            'isKeep': self.isKeep,
            'isIntegration': self.isIntegration,
        }
